--社交(改为关卡榜)
---@class Billboard_SocialPanel : UIComponentBase
local SocialPanel = Class("SocialPanel",UIComponentBase)
local t = SocialPanel

function t:OnCreate()
    self.txt_title = self:GetChild("Title")
    self.nameTxt = self:GetChild("name")
    self.txt_today_rank = self:GetChild("todayrank")
    self.rankTxt = self:GetChild("rank") 
    self.txt_point_name = self:GetChild("text")   -- 名称
    self.pointTxt = self:GetChild("point")        -- 值
    self.changeTxt = self:GetChild("change")      -- 说明文本

    self.btnRank = self:GetChild("LookRank")      -- 查看排名按钮
    self.btnReward = self:GetChild("RewardGet")   -- 查看奖励按钮

    self.head = self:GetChild("head")
    self.headIcon = self:GetChild("head/HeadIcon",self.head)
    self.headFrame = self:GetChild("frame",self.head)

    self:RegisterOnClick(self.btnRank,self.ClickRank)
    self:RegisterOnClick(self.btnReward,self.ClickReward)
    self:RegisterOnClick(self.head,self.ClickHead)

    -- self.rankType = logicMgr.NoticeManager.RankType.social
    self.rankType = logicMgr.NoticeManager.RankType.Level
    self.myRank = nil
    self:InitData()
end

function t:InitData()
    self.changeTxt.text = logicMgr.NoticeManager.GetAbnormalChangeStr(self.rankType)
    self:RefreshAlertPlayer()
    self:RefreshReward()
    --无异动显示第一名
    if not logicMgr.NoticeManager.HasAlertChange(self.rankType) then
        NoticeHandler.ReqMainRankInfo(self.rankType)
    end
end

-- function t:OnEnable() 
-- end
-- function t:OnDisable()
-- end
-- function t:OnDestroy()
-- end

---@param player NoticePlayerInfo
function t:RefreshAlertPlayer(player)
    if not player then
        player = logicMgr.NoticeManager.GetAlertPlayer(self.rankType)
    end
 
    if player then
        self.firstPlayerId = player.playerId
        self.rankTxt.text = player.rank
        self.nameTxt.text = player.name
        local param = tonumber(player.param)
        local param1 = math.floor(param/100)
        local param2 = param - param1*100
        self.pointTxt.text = string.format("%s-%s", param1, param2)
        self:SetPlayerIcon(self.headIcon,player.headId,self.headFrame,player.picFrame)
    else
        self.firstPlayerId = ""
        self.rankTxt.text = "0"
        self.nameTxt.text =  getLanguage("BillboardRankNeedPlayer")
        self.pointTxt.text = "0-0"
        self.headIcon.url = EnumConst.AtlasPath.CommonUI.."icon_hero_kb91"
        self.headFrame.url = ""
    end
end

function t:GetAllData()
    local infos = logicMgr.NoticeManager.GetMainRanksByType(self.rankType)
 
    if infos.ranks[1] == nil then
        self.changeTxt.text = logicMgr.NoticeManager.GetChangeStr(self.rankType, NoticeManager.NO_MAIN_CHOOSE_TYPE)
        return
    end
    local first = infos.ranks[1]
    if logicMgr.NoticeManager.HasAlertChange(self.rankType) then
        return
    end
    self:RefreshAlertPlayer(first)
end

function t:RefreshReward()
    self.btnReward.visible = logicMgr.NoticeManager.HasReward(self.rankType )
end

function t:ClickRank()
    logicMgr.UIShowManager.ShowUISocialRank()
end

function t:ClickReward()
    NoticeHandler.ReqChangeAwardByType(self.rankType)
end

function t:ClickHead()
    if self.firstPlayerId ~= "" then
        roleInfoHandler.sendreqOtherRoleInfo(self.firstPlayerId);
    end
end
return t